package com.ruoyi.voucher.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.voucher.domain.Voucher;
import com.ruoyi.voucher.domain.vo.QueryVoucherVo;
import com.ruoyi.voucher.domain.dto.req.VoucherReqDTO;
import com.ruoyi.voucher.domain.dto.resp.VoucherRespDTO;
import com.ruoyi.voucher.service.IVoucherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 凭证管理Controller
 * 
 * @author ruoyi
 * @date 2025-02-21
 */
@RestController
@RequestMapping("/voucher")
public class VoucherController extends BaseController
{
    @Autowired
    private IVoucherService voucherService;


    /**
    * @Description: 凭证列表
    * @Param: [queryVoucherDTO]
    * @return: com.ruoyi.common.core.web.page.TableDataInfo
    * @Author: Yuan
    * @Date: 2025/2/27
    */
    @GetMapping("/voucherList")
    public TableDataInfo voucherList(QueryVoucherVo queryVoucherVo){
        startPage();
        List<VoucherRespDTO> list = voucherService.getVoucherList(queryVoucherVo);
        return getDataTable(list);
    }

    @GetMapping("/voucherList1")
    public TableDataInfo voucherList1(QueryVoucherVo queryVoucherVo){
        startPage();
        List<Voucher> list = voucherService.selectVoucherList1(queryVoucherVo);
        return getDataTable(list);
    }

    /**
     * 查询凭证管理列表
     */
    @RequiresPermissions("voucher:voucher:list")
    @GetMapping("/list")
    public TableDataInfo list(Voucher voucher)
    {
        startPage();
        List<Voucher> list = voucherService.selectVoucherList(voucher);
        return getDataTable(list);
    }

    /**
    * @Description: 新增凭证
    * @Param: [voucherReqDTO]
    * @return: com.ruoyi.common.core.web.domain.AjaxResult
    * @Author: Yuan
    * @Date: 2025/2/27
    */
    @PostMapping("/addVoucher")
    public AjaxResult addVoucher(@RequestBody @Valid VoucherReqDTO voucherReqDTO) throws Exception {
        return toAjax(voucherService.addVoucher(voucherReqDTO));
    }

    /**
     * 导出凭证管理列表
     */
    @RequiresPermissions("voucher:voucher:export")
    @Log(title = "凭证管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Voucher voucher)
    {
        List<Voucher> list = voucherService.selectVoucherList(voucher);
        ExcelUtil<Voucher> util = new ExcelUtil<Voucher>(Voucher.class);
        util.exportExcel(response, list, "凭证管理数据");
    }

    /**
     * 获取凭证管理详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) throws Exception {
        return success(voucherService.selectVoucherById(id));
    }

    /**
     * 新增凭证管理
     */
    @RequiresPermissions("voucher:voucher:add")
    @Log(title = "凭证管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Voucher voucher)
    {
        return toAjax(voucherService.insertVoucher(voucher));
    }

    /**
     * 修改凭证管理
     */
    @RequiresPermissions("voucher:voucher:edit")
    @Log(title = "凭证管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Valid VoucherReqDTO voucherReqDTO) throws Exception {
        return toAjax(voucherService.updateVoucher(voucherReqDTO));
    }

    /**
     * 删除凭证管理
     */
    @RequiresPermissions("voucher:voucher:remove")
    @Log(title = "凭证管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(voucherService.deleteVoucherByIds(ids));
    }
}
